#ifndef DATABASEMANAGER_H
#define DATABASEMANAGER_H

#include <QObject>
#include <QJsonObject>
#include <QMap>
#include <QtSql>
class DatabaseManager : public QObject
{
public:
    DatabaseManager(QObject* parent=nullptr);
    ~DatabaseManager();
private:
    QJsonObject database_config_;
    QString database_type_="QSQLITE";
    QString connectionm_name_="maindb";
    QString database_filename_="mainDb.db";
    QSqlDatabase database_;
    QSqlQuery*  query_;
    QStringList table_name_list_;
public:
    QStringList table_name_list() const;
public:


    QSqlDatabase* database();

    /**
     * @brief loadConfig
     * @param root_config_obj_
     */
    void loadConfig(QJsonObject root_config_obj_);
    /**
     * @brief initDatabaseFromConfig
     */

    void insertField(QString table_name, QJsonObject field_config);

    void initDatabaseFromConfig();
    /**
     * @brief insertRecord
     * @param table_name
     * @param attr_map
     */
    bool insertRecord(const QString &table_name, const QMap<QString,QString> &attr_map);
    /**
     * @brief reviseRecord
     * @param table_name
     * @param id_map
     * @param attr_map
     * @return
     */
    bool reviseRecord(const QString &table_name, const QMap<QString,QString> &id_map, const QMap<QString,QString> &attr_map, QStringList revise_list=QStringList());
    /**
     * @brief reviseRecord
     * @param table_name
     * @param id_list
     * @param attr_map
     * @return
     */
    bool reviseRecord(const QString &table_name, const QStringList &id_list, const QMap<QString,QString> &attr_map, QStringList revise_list=QStringList());
    /**
     * @brief reviseRecord
     * @param table_name
     * @param id
     * @param attr_map
     * @return
     */
    bool reviseRecord(const QString &table_name, const QString &id, const QMap<QString, QString> &attr_map, QStringList revise_list=QStringList());

    QString getQueryFilterFromJson(const QJsonObject & query_object);

    QString getQueryFilterFromMapList(const QList<QMap<QString,QString>>);

    bool queryRecord(const QString table_name, const QJsonObject & query_object);

    bool fetchAllRecord();

    bool queryRecordInTable(QSqlTableModel* table_mtestodel,const QString & filter_str);

    bool queryRecordInTable(QSqlTableModel* table_model,const QJsonObject & query_object);

    void countForFormula(QString filter);

    QString getSelectString(const QJsonObject &query_object, const QJsonArray &user_filter_array=QJsonArray());

    void createView(const QJsonObject &query_object, const QJsonArray user_filter_array=QJsonArray());

    void updateColumn(QString table_name, QStringList column_name, QString value, QString filter);

    void createTrigger(const QJsonObject &query_object);

    void getViewInQueryModel(QSqlQueryModel &model, QString view_name, QJsonArray filter_list=QJsonArray());

    void getQueryResult(QSqlQueryModel &model, QJsonObject query_object, QJsonArray user_filter_array=QJsonArray());

    bool queryRecord(const QString table_name, const QList<QMap<QString, QString> > &query_object);

    void wirteTosv(QString filename, QString table_name, QStringList out_field_list, QStringList out_title_list, QString filter, QString out_group);

};

#endif // DATABASEMANAGER_H
